package com.itheima.reggie.controller;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.IAddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 地址管理
 */
@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private IAddressBookService addressBookService;
    /**
     * 新增地址
     * @param addressBook
     * @return
     */
    @PostMapping
    public R save(HttpSession session, @RequestBody AddressBook addressBook) {
        User loginPhone = (User) session.getAttribute("loginPhone");
        Long id = loginPhone.getId();
        log.info("addressBook:{}",addressBook);
        addressBook.setUserId(id);
        addressBookService.save(addressBook);
        return R.success("添加成功");
    }
    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public R setDefault(@RequestBody AddressBook addressBook) {
        log.info("addressBook:{}",addressBook);
        LambdaUpdateWrapper<AddressBook> qw = new LambdaUpdateWrapper<>();
        qw.set(AddressBook::getIsDefault,0);
        qw.eq(AddressBook::getUserId, UserHolder.get());
        addressBookService.update(qw);
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success("设置成功");
    }
    /**
     * 查询用户指定的全部地址
     * @param session
     * @return
     */
    @GetMapping("/list")
    public R get(HttpSession session){
        User loginPhone = (User) session.getAttribute("loginPhone");
        Long id = loginPhone.getId();
        LambdaQueryWrapper<AddressBook> qw= new LambdaQueryWrapper<>();
        qw.eq(AddressBook::getUserId,id);
        List<AddressBook> list = addressBookService.list(qw);
        return R.success(list);
    }
    /**
     * 查询默认地址
     * @return
     */
    @GetMapping("/default")
    public R<AddressBook> get(){
        LambdaQueryWrapper<AddressBook> qw=new LambdaQueryWrapper<>();
        qw.eq(AddressBook::getUserId,UserHolder.get());
        qw.eq(AddressBook::getIsDefault,1);
        AddressBook one = addressBookService.getOne(qw);
        if (one!=null){
            return R.success(one);
        }
        return R.error("查询失败");
    }
    /**
     * 删除地址
     * @param ids
     * @return
     */
    @DeleteMapping
    public R delete(@RequestParam List<Long> ids){
        for (Long id : ids) {
            LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
            qw.eq(AddressBook::getUserId,id);
            addressBookService.remove(qw);
        }
//        addressBookService.removeByIds(ids);
        return R.success("删除成功");
    }
    /**
     * 根据id 查询地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R get(@PathVariable Long id){
        AddressBook byId = addressBookService.getById(id);
        if (byId !=null){
            return R.success(byId);
        }
        return R.error("查询失败");
    }
    /**
     * 修改地址
     * @param addressBook
     * @return
     */
    @PutMapping
    public R update(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return R.success(null);
    }
}